/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2015 - 2025 CCBlueX
 *
 * LiquidBounce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LiquidBounce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
 */
package net.ccbluex.liquidbounce.features.module.modules.exploit

import net.ccbluex.liquidbounce.config.types.NamedChoice
import net.ccbluex.liquidbounce.features.module.Category
import net.ccbluex.liquidbounce.features.module.ClientModule
import net.ccbluex.liquidbounce.utils.client.PlainText
import net.ccbluex.liquidbounce.utils.client.chat
import net.ccbluex.liquidbounce.utils.client.markAsError
import net.minecraft.network.packet.c2s.play.PlayerInteractEntityC2SPacket
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket
import net.minecraft.network.packet.c2s.play.UpdateSelectedSlotC2SPacket
import net.minecraft.util.Hand
import kotlin.random.Random

/**
 * Kick module
 *
 * Kick yourself from the server.
 */
object ModuleKick : ClientModule("Kick", Category.EXPLOIT, disableActivation = true) {

    init {
        doNotIncludeAlways()
    }

    private val mode by enumChoice("Mode", KickModeEnum.QUIT)

    override fun onEnabled() {
        if (mc.isIntegratedServerRunning) {
            chat(markAsError(message("cantEnableInSingleplayer")))
            return
        }

        kick(mode)
    }

    /**
     * Kick client player
     *
     * @warning Only use on multiplayer! This function can cause the game to crash when used on a integrated server.
     */
    fun kick(mode: KickModeEnum) = mode.run()

    @Suppress("unused")
    enum class KickModeEnum(override val choiceName: String, val run: () -> Unit) : NamedChoice {
        QUIT("Quit", {
            world.disconnect(PlainText.EMPTY)
        }),
        INVALID_PACKET("InvalidPacket", {
            network.sendPacket(
                PlayerMoveC2SPacket.PositionAndOnGround(
                    Double.NaN,
                    Double.NEGATIVE_INFINITY,
                    Double.POSITIVE_INFINITY,
                    !player.isOnGround,
                    !player.horizontalCollision
                )
            )
        }),
        SELF_HURT("SelfHurt", {
            network.sendPacket(PlayerInteractEntityC2SPacket.attack(player, player.isSneaking))
        }),
        ILLEGAL_CHAT("IllegalChat", {
            network.sendChatMessage("§${Random.nextInt()}§${Random.nextInt()}§")
        }),
        ILLEGAL_SWTTCHITEM("IllegalSwitchitem", {
            network.sendPacket(UpdateSelectedSlotC2SPacket(-1))
        }),
        ILLEGAL_INTERACT("IllegalInteract", {
            network.sendPacket(PlayerInteractEntityC2SPacket.interact(player, player.isSneaking,Hand.MAIN_HAND))
        }),
    }

}
